package com.pctx.bibaregisterlogin.service.impl;

import com.pctx.bibacommon.result.Result;
import com.pctx.bibacommon.util.MD5Util;
import com.pctx.bibaregisterlogin.dao.UserMapper;
import com.pctx.bibaregisterlogin.pojo.User;
import com.pctx.bibaregisterlogin.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("loginService")
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result verifyLogin(String usernameOrMobile, String password) throws Exception {
        User user1 = userMapper.selectByMobile(usernameOrMobile);//手机号
        User user2 = userMapper.selectByUsername(usernameOrMobile);//用户名
        Map resMap = new HashMap();
        if (user1 == null && user2 == null) {
            //  resMap.put(0, "用户名或手机号输入错误");
            return Result.error("用户名或手机号输入错误");
           // return Result.success(null, "用户名或手机号输入错误");
        } else if (user1 != null) {
            String password1 = MD5Util.getMD5(password);
            if (password1.equals(user1.getPassword())) {
                // resMap.put(1, "登录成功");
                return Result.success(user1,"登陆成功");
            } else {
                return Result.error("密码输入错误");
                //return Result.success(null, "密码输入错误");
            }
        } else {
            String password2 = MD5Util.getMD5(password);
            if (password2.equals(user2.getPassword())) {
                //  resMap.put(1, "登录成功");
                return Result.success(user1,"登陆成功");
            } else {
                return Result.error("密码输入错误");
                //return Result.success(null, "密码输入错误");
            }
        }

    }

    @Override
    public Result changeUserPass(String mobile, String password) throws Exception {
        User user = userMapper.selectByMobile(mobile);
        Date date = new Date();
      //  Timestamp nowdate = new Timestamp(date.getTime());
        user.setUpdateTime(date);
        user.setRealpassword(password);
        user.setPassword(MD5Util.getMD5(password));
        Boolean flag = userMapper.updateByMobileSelective(user) > 0;
        if (flag) {
            return Result.success(null, "用户密码修改成功");
        } else {
            return Result.error("用户密码修改异常");
        }
    }
}
